package com.unitd.base.frame.exception;

import com.unitd.base.frame.enums.BaseEnum;
import com.unitd.base.frame.enums.ResultEnum;
import lombok.Data;

import java.text.MessageFormat;

/**
 * 自定义的异常信息类, 用于按照自定义规则抛出异常信息(其他自定义的异常类可以继承自此基类)
 *
 * @author hudan
 * @version 1.0
 * @filename UnitdException.java
 * @copyright www.unitd.com
 * @date 2019-01-25
 */
@Data
public class UnitdException extends RuntimeException {

    /**
     * 错误代码
     */
    private String code;

    public UnitdException(UnitdException e) {
        super(e.getMessage());
        this.code = e.getCode();
    }

    public UnitdException(String message) {
        super(message);
    }

    public UnitdException(BaseEnum resultEnum) {
        super(resultEnum.getCode());
        this.code = resultEnum.getCode();
    }

    public UnitdException(Throwable throwable) {
        super(throwable);
        this.code = ResultEnum.SYSTEM_ERROR.getCode();
    }

    public UnitdException(String code, String message) {
        super(message);
        this.code = code;
    }

    public UnitdException(String message, Throwable throwable) {
        super(message, throwable);
        this.code = ResultEnum.SYSTEM_ERROR.getCode();
    }

    /**
     * 根据需要进行格式化的错误消息 反馈相应结果
     *
     * @param resultEnum 异常枚举对象信息
     * @param arguments  错误描述信息占位数据
     */
    public UnitdException(BaseEnum resultEnum, Object... arguments) {
        super(MessageFormat.format(resultEnum.getCode(), arguments));

        this.code = resultEnum.getCode();
    }

    public UnitdException(BaseEnum resultEnum, String messasge) {
        super(messasge);

        this.code = resultEnum.getCode();
    }
}